package com.doge.walknovel.business.reader

import android.graphics.Typeface
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.paging.PagedList
import com.doge.walknovel.api.*
import com.doge.walknovel.api.model.ChapterInfo
import com.doge.walknovel.business.novel.NovelChapterPosHelper
import com.doge.walknovel.core.base.LoadStatus
import com.doge.walknovel.core.base.adapters.Cell
import com.doge.walknovel.core.base.adapters.toLiveData
import com.doge.walknovel.utils.isMainThread
import com.doge.walknovel.utils.runOnIO

class ReaderViewModel constructor(private val repository: ReaderRepository) : ViewModel() {
    val centerEvents = MutableLiveData<Any?>()
    val readerDisplayingChapter = MutableLiveData<ChapterInfo?>() // for comments
    val dataList: LiveData<PagedList<Cell>> = repository.dataList

    fun displayMenus() {
        require(isMainThread())
        centerEvents.value = Any()
        centerEvents.value = null
    }

    fun changeSize(value: Int) = runOnIO { repository.changeSize(value) }
    fun changeLight(light: Boolean) = runOnIO { repository.changeLight(light) }
    fun changeBgColor(bgColor: String) = runOnIO { repository.changeBgColor(bgColor)  }
    fun changeTypeface(typeface: Typeface) = runOnIO { repository.changeTypeface(typeface) }

    fun requestRecordChapter(chapter: ChapterInfo?) {
        if (chapter == null) return
        if (chapter.chapterId == readerDisplayingChapter.value?.chapterId) return
        readerDisplayingChapter.value = chapter
        NovelChapterPosHelper.saveCache(repository.novelId, chapter.chapterId)
        // id 去重复
        api
            .novelChapterRecordRead(chapter.chapterId)
            .subscribeOnIO()
            .subscribe(object : ObserverAdapter<Any?>() {
                override fun onError(e: Throwable) = Unit
            })
    }

    fun requestUnlock(chapter: ChapterInfo?) = repository.requestUnlock(chapter)
    fun redirectToChapterProgress(progress: Float?) = repository.redirectToChapterProgress(progress)
    fun redirectToChapterId(chapterId: Int?) = repository.redirectToChapterId(chapterId, Direction.BOTH)

    //////////////////////////////////////
    val menuScrollToChapterId = MutableLiveData<Int?>()
    val chapterLoading: LiveData<LoadStatus> = repository.chapterLoading
    val chapterDataList = repository.chapterDataSource().toLiveData(
        cellId = { it.id },
        requestMore = { repository.requestChapters(false) })

    fun requestChapters() {
        repository.requestChapters(true)
    }

    fun scrollToChapter(value: Int?) {
        if (value == null) return
        require(isMainThread())
        menuScrollToChapterId.value = value
        menuScrollToChapterId.value = null
    }
}


class ReaderViewModelFactory(private val requestId: Int) : ViewModelProvider.Factory {
    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        val repository = ReaderRepository(requestId)
        if (modelClass.isAssignableFrom(ReaderViewModel::class.java)) {
            return ReaderViewModel(repository) as T
        }
        //noinspection unchecked
        throw IllegalArgumentException("Unknown ViewModel class")
    }
}

